<template>
  <div class="app-container">
    <!--对象搜索-->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="product">
        <y-select v-model="queryParams.product" placeholder="请选择产品名称" :optionsFunc="listProduct" />
      </el-form-item>
      <el-form-item label="接口类型" prop="type">
        <y-select v-model="queryParams.type" placeholder="请选择接口类型" :options="sys_http_method" />
      </el-form-item>
      <el-form-item label="接口名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入接口名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <y-select v-model="queryParams.status" placeholder="请选择接口状态" :options="sys_enable_disable" />
      </el-form-item>
      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--对象操作-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['rms:protocol:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleEdit"
          v-hasPermi="['rms:protocol:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['rms:protocol:delete']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Upload" @click="handleImport" v-hasPermi="['rms:protocol:import']">导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <y-export :url="'rms/protocol/type/export/'" :ids="ids" :filename="'protocol_type'"
          v-hasPermi="['rms:protocol:export']" />
      </el-col>
      <y-right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" />
    </el-row>

    <!--对象列表-->
    <el-table ref="tableRef" v-loading="loading" row-key="id" :data="typeList" @selection-change="handleSelectionChange"
      border>
      <el-table-column type="selection" width="50" align="center" reserve-selection />
      <el-table-column label="序号" width="100" align="center" type="index">
        <template #default="scope">
          {{ scope.$index + (queryParams.page - 1) * queryParams.limit + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="产品名称" align="center" prop="product.label" v-if="columns[0].visible"
        :show-overflow-tooltip="true" sortable />
      <el-table-column label="接口名称" align="center" prop="name" v-if="columns[1].visible" :show-overflow-tooltip="true"
        sortable />
      <el-table-column label="接口类型" align="center" prop="type" v-if="columns[2].visible" :show-overflow-tooltip="true"
        sortable />
      <el-table-column label="接口地址" align="center" prop="url" v-if="columns[3].visible" :show-overflow-tooltip="true"
        sortable />
      <el-table-column label="状态" align="center" prop="status" v-if="columns[4].visible" sortable>
        <template #default="scope">
          <y-dict-tag :options="sys_enable_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建人" width="100" align="center" prop="creator_name" v-if="columns[5].visible" sortable
        :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="create_time" v-if="columns[6].visible" sortable width="160">
        <template #default="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="100" align="center">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleEdit(scope.row)"
              v-hasPermi="['rms:protocol:edit']" />
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
              v-hasPermi="['rms:protocol:delete']" />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!--列表分页-->
    <y-pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.limit"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <au-interface :id="id" v-model="open" :title="title" :operate="operate" :getMethod="getInterface"
      :addMethod="addInterface" :updateMethod="updateInterface" @submited="submited" @close="open = false">
    </au-interface>

    <!-- 导入对话框-->
    <y-import :upload="upload" @submited="submited" />
  </div>
</template>

<script setup name="ProtocolList">
import {
  listInterface,
  getInterface,
  delInterface,
  addInterface,
  updateInterface,
} from "@/api/rms/interface";
import { listProduct } from "@/api/pms/product";
import AuInterface from "./components/AuInterface";

const { proxy } = getCurrentInstance();
const { sys_enable_disable, sys_http_method } = proxy.useDict(
  "sys_enable_disable",
  "sys_http_method"
);

const id = ref(null);
const ids = ref([]);
const open = ref(false);
const title = ref("");
const loading = ref(false);
const operate = ref("add");
const showSearch = ref(false);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const typeList = ref([]);
const dateRange = ref([]);

/*** 导入参数 */
const upload = reactive({
  // 是否显示弹出层
  open: false,
  // 弹出层标题
  title: "",
  // 上传的地址
  url: import.meta.env.VITE_APP_BASE_API + "/rms/protocol/type/import/",
});

// 列显隐信息
const columns = ref([
  { key: 0, label: `所属产品`, visible: true },
  { key: 1, label: `接口名称`, visible: true },
  { key: 2, label: `接口类型`, visible: true },
  { key: 3, label: `接口地址`, visible: true },
  { key: 4, label: `状态`, visible: true },
  { key: 5, label: `创建人`, visible: true },
  { key: 6, label: `创建时间`, visible: true },
]);

const data = reactive({
  queryParams: {
    product: undefined,
    name: undefined,
    type: undefined,
    status: undefined,
    page: 1,
    limit: 10,
  },
});

const { queryParams } = toRefs(data);

/** 查询接口类型列表 */
function getList() {
  loading.value = true;
  listInterface(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (res) => {
      typeList.value = res.data;
      total.value = res.total;
      loading.value = false;
    }
  );
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 搜索按钮操作 */
function handleQuery(params = {}) {
  queryParams.value = Object.assign(queryParams.value, params.value);
  queryParams.value.page = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  operate.value = "add";
  open.value = true;
  title.value = "添加接口";
}

/** 修改按钮操作 */
function handleEdit(row) {
  id.value = row.id || ids.value[0];
  operate.value = "update";
  open.value = true;
  title.value = "修改接口";
}

/** 导入按钮操作 */
function handleImport() {
  upload.title = "接口导入";
  upload.open = true;
}

/** 删除按钮操作 */
async function handleDelete(row) {
  try {
    if (row.id) {
      await proxy.$modal.confirm(`确认是否删除 "${row.name}" ？`);
    }
    else {
      await proxy.$modal.confirm("确认是否删除所有已勾选信息？");
    }
    ids.value = row.id ? [row.id] : ids.value;
    await delInterface(ids.value);
    proxy.$modal.msgSuccess("删除成功");
    proxy.$refs.tableRef.clearSelection()
    getList();
  } catch (error) {
    console.error(error);
  }
}

/** 添加/修改对象后触发事件 */
function submited() {
  getList();
}

/** Expose方法 */
defineExpose({
  handleQuery,
});

onMounted(() => {
  getList();
})
</script>
